#!/usr/bin/env python3
# -*- coding:utf-8 -*-
__author__ = 'Jinyang Shao'

from socket import *
import time

serverName = 'localhost'
serverPort = 12000

clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.settimeout(1.0)

lost = 0

for i in range(10):
    message = "Ping " + str(i+1) + " " + str(time.time())
    try:
        begin = time.time()
        clientSocket.sendto(message.encode(), (serverName, serverPort))
        modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
        print(modifiedMessage, "RTT:"+str(time.time()-begin))
    except timeout:
        lost += 1
        print("Request timed out")
    except KeyboardInterrupt:
        if i != 0:
            print("The lost rate:", lost/i)
        exit(0)

print("The lost rate:", lost/10)
